package com.hhh.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.Mapper.EmployeeMapper;
import com.hhh.Pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
public class EmployeeService implements com.hhh.Service.EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    //    添加员工
    @Override
    public Integer Insert(Employee employee, long id) {
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);
        byte[] password = "123456".getBytes();
        String passwordBymd5 = DigestUtils.md5DigestAsHex(password);
        employee.setPassword(passwordBymd5);
        return employeeMapper.insert(employee);
    }

    //    查询员工
    @Override
    public IPage<Employee> SelectList(long page, long pagesize, String name) {
        IPage<Employee> iPage = new Page<>();
        iPage.setPages(page);
        iPage.setSize(pagesize);
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        if (name != null) {
            wrapper.like("name", name);
        }
        wrapper.orderByDesc("update_time");
//        IPage<Employee> returnipage=employeeMapper.selectPage(iPage,wrapper);
//        List<Employee> arr= returnipage.getRecords();
        return employeeMapper.selectPage(iPage, wrapper);
    }


    //    修改员工数据
    @Override
    public Integer Update(Employee employee, long UpdateUserId) {
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(UpdateUserId);
        return employeeMapper.updateById(employee);
    }

    @Override
    public Employee SelectById(long id) {
        return employeeMapper.selectById(id);
    }


}
